/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.client.ZContact;
/*     */ import com.zimbra.client.ZMailbox;
/*     */ import com.zimbra.client.ZMailbox.ContactSortBy;
/*     */ import com.zimbra.client.ZMailbox.ZAttachmentInfo;
/*     */ import com.zimbra.client.ZMailbox.ZImportContactsResult;
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.soap.SoapFaultException;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.mailbox.Contact;
/*     */ import com.zimbra.cs.mailbox.Contact.Attachment;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxManager;
/*     */ import java.io.InputStream;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Set;
/*     */ import javax.activation.DataHandler;
/*     */ import javax.mail.util.ByteArrayDataSource;
/*     */ import junit.framework.TestCase;
/*     */ import org.junit.Assert;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestContacts
/*     */   extends TestCase
/*     */ {
/*  55 */   private static final String NAME_PREFIX = TestContacts.class.getSimpleName();
/*     */   private static final String USER_NAME = "user1";
/*     */   String mOriginalMaxContacts;
/*     */   
/*     */   public void setUp() throws Exception
/*     */   {
/*  61 */     cleanUp();
/*  62 */     this.mOriginalMaxContacts = TestUtil.getAccountAttr("user1", "zimbraContactMaxNumEntries");
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testMaxContacts()
/*     */     throws Exception
/*     */   {
/*  70 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/*  71 */     List<ZContact> contacts = mbox.getAllContacts(null, ZMailbox.ContactSortBy.nameAsc, false, null);
/*  72 */     int max = contacts.size() + 2;
/*  73 */     TestUtil.setAccountAttr("user1", "zimbraContactMaxNumEntries", Integer.toString(max));
/*  74 */     Map<String, String> attrs = new HashMap();
/*     */     
/*  76 */     for (int i = 1; i <= 10; i++) {
/*  77 */       attrs.put("fullName", NAME_PREFIX + " testMaxContacts" + i);
/*     */       try {
/*  79 */         mbox.createContact(Integer.toString(7), null, attrs);
/*     */       } catch (SoapFaultException e) {
/*  81 */         assertEquals("mail.TOO_MANY_CONTACTS", e.getCode());
/*  82 */         break;
/*     */       }
/*     */     }
/*  85 */     assertEquals("Unexpected contact number", 3, i);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testServerAttachment()
/*     */     throws Exception
/*     */   {
/*  94 */     byte[] data = "test".getBytes();
/*  95 */     ByteArrayDataSource ds = new ByteArrayDataSource(data, "text/plain");
/*  96 */     ds.setName("attachment.txt");
/*  97 */     DataHandler dh = new DataHandler(ds);
/*  98 */     Contact.Attachment attach = new Contact.Attachment(dh, "attachment", data.length);
/*     */     
/*     */ 
/* 101 */     attach = new Contact.Attachment(dh, "attachment");
/* 102 */     checkServerAttachment(data, attach);
/*     */     
/*     */ 
/* 105 */     attach = new Contact.Attachment(data, "text/plain", "attachment", "attachment.txt");
/* 106 */     checkServerAttachment(data, attach);
/*     */   }
/*     */   
/*     */   private void checkServerAttachment(byte[] expected, Contact.Attachment attach) throws Exception
/*     */   {
/* 111 */     TestUtil.assertEquals(expected, attach.getContent());
/* 112 */     TestUtil.assertEquals(expected, ByteUtil.getContent(attach.getInputStream(), 4));
/* 113 */     assertEquals(expected.length, attach.getSize());
/* 114 */     assertEquals("text/plain", attach.getContentType());
/* 115 */     assertEquals("attachment", attach.getName());
/* 116 */     assertEquals("attachment.txt", attach.getFilename());
/*     */   }
/*     */   
/*     */   public void testContactAttachments() throws Exception
/*     */   {
/* 121 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/*     */     
/*     */ 
/* 124 */     Map<String, String> attrs = new HashMap();
/* 125 */     attrs.put("fullName", NAME_PREFIX + " testAttachments");
/*     */     
/* 127 */     String attachment1Text = "attachment 1";
/* 128 */     int timeout = 60000;
/* 129 */     String folderId = Integer.toString(7);
/*     */     
/* 131 */     String attachment1Id = mbox.uploadAttachment("attachment.txt", attachment1Text.getBytes(), "text/plain", timeout);
/* 132 */     Map<String, ZMailbox.ZAttachmentInfo> attachments = new HashMap();
/* 133 */     ZMailbox.ZAttachmentInfo info = new ZMailbox.ZAttachmentInfo().setAttachmentId(attachment1Id);
/* 134 */     attachments.put("attachment1", info);
/* 135 */     ZContact contact = mbox.createContact(folderId, null, attrs, attachments);
/*     */     
/*     */ 
/* 138 */     assertTrue(contact.getAttachmentNames().contains("attachment1"));
/* 139 */     byte[] data = getAttachmentData(contact, "attachment1");
/* 140 */     assertEquals(attachment1Text, new String(data));
/*     */     
/*     */ 
/* 143 */     String attachment2Text = "attachment 2";
/* 144 */     String attachment2Id = mbox.uploadAttachment("attachment.txt", attachment2Text.getBytes(), "text/plain", timeout);
/* 145 */     attachments.clear();
/* 146 */     info.setAttachmentId(attachment2Id);
/* 147 */     attachments.put("attachment2", info);
/* 148 */     contact = mbox.modifyContact(contact.getId(), false, null, attachments);
/*     */     
/*     */ 
/* 151 */     data = getAttachmentData(contact, "attachment2");
/* 152 */     assertEquals(attachment2Text, new String(data));
/*     */     
/*     */ 
/* 155 */     String newAttachment2Text = "new attachment 2";
/* 156 */     String newAttachment2Id = mbox.uploadAttachment("attachment.txt", newAttachment2Text.getBytes(), "text/plain", timeout);
/* 157 */     info.setAttachmentId(newAttachment2Id);
/* 158 */     contact = mbox.modifyContact(contact.getId(), false, null, attachments);
/*     */     
/*     */ 
/* 161 */     data = getAttachmentData(contact, "attachment2");
/* 162 */     assertEquals(newAttachment2Text, new String(data));
/*     */     
/*     */ 
/* 165 */     info.setAttachmentId(null);
/* 166 */     info.setPartName(contact.getAttachmentPartName("attachment2"));
/* 167 */     attachments.clear();
/* 168 */     attachments.put("attachment3", info);
/* 169 */     contact = mbox.modifyContact(contact.getId(), false, null, attachments);
/*     */     
/*     */ 
/* 172 */     data = getAttachmentData(contact, "attachment2");
/* 173 */     assertEquals(newAttachment2Text, new String(data));
/* 174 */     data = getAttachmentData(contact, "attachment3");
/* 175 */     assertEquals(newAttachment2Text, new String(data));
/*     */     
/*     */ 
/* 178 */     String attachment4Text = "attachment 4";
/* 179 */     String attachment4Id = mbox.uploadAttachment("attachment.txt", attachment4Text.getBytes(), "text/plain", timeout);
/* 180 */     info.setAttachmentId(attachment4Id);
/* 181 */     info.setPartName(null);
/* 182 */     attachments.clear();
/* 183 */     attachments.put("attachment4", info);
/* 184 */     contact = mbox.modifyContact(contact.getId(), true, attrs, attachments);
/*     */     
/*     */ 
/* 187 */     Set<String> names = contact.getAttachmentNames();
/* 188 */     assertEquals(1, names.size());
/* 189 */     assertTrue(names.contains("attachment4"));
/* 190 */     data = getAttachmentData(contact, "attachment4");
/* 191 */     assertEquals(attachment4Text, new String(data));
/*     */     
/*     */ 
/* 194 */     info.setAttachmentId(null);
/* 195 */     contact = mbox.modifyContact(contact.getId(), false, attrs, attachments);
/* 196 */     assertEquals(0, contact.getAttachmentNames().size());
/*     */     
/*     */ 
/* 199 */     attachment4Id = mbox.uploadAttachment("attachment.txt", attachment4Text.getBytes(), "text/plain", timeout);
/* 200 */     info.setAttachmentId(attachment4Id);
/* 201 */     info.setPartName(null);
/* 202 */     attachments.clear();
/* 203 */     attachments.put("attachment4", info);
/* 204 */     contact = mbox.modifyContact(contact.getId(), false, attrs, attachments);
/*     */     
/*     */ 
/* 207 */     names = contact.getAttachmentNames();
/* 208 */     assertEquals(1, names.size());
/* 209 */     assertTrue(names.contains("attachment4"));
/* 210 */     data = getAttachmentData(contact, "attachment4");
/* 211 */     assertEquals(attachment4Text, new String(data));
/*     */   }
/*     */   
/*     */   public void testMoveContact() throws Exception
/*     */   {
/* 216 */     String USER2_NAME = "user2";
/* 217 */     ZMailbox zmbx = TestUtil.getZMailbox("user1");
/* 218 */     Account acct = Provisioning.getInstance().get(Key.AccountBy.name, TestUtil.getAddress("user1"));
/*     */     
/*     */ 
/*     */ 
/* 222 */     Map<String, String> attrs = new HashMap();
/* 223 */     attrs.put("fullName", NAME_PREFIX + " testMoveContact");
/*     */     
/* 225 */     String attachment1Text = "attachment 1";
/* 226 */     int timeout = 60000;
/* 227 */     String folderId = Integer.toString(7);
/*     */     
/* 229 */     String attachment1Id = zmbx.uploadAttachment("file1.txt", attachment1Text.getBytes(), "text/plain", timeout);
/* 230 */     Map<String, ZMailbox.ZAttachmentInfo> attachments = new HashMap();
/* 231 */     ZMailbox.ZAttachmentInfo info = new ZMailbox.ZAttachmentInfo().setAttachmentId(attachment1Id);
/* 232 */     attachments.put("file1", info);
/* 233 */     ZContact contact = zmbx.createContact(folderId, null, attrs, attachments);
/*     */     
/*     */ 
/* 236 */     attrs = new HashMap();
/* 237 */     attrs.put("phone", NAME_PREFIX + " testMoveContact");
/* 238 */     zmbx.modifyContact(contact.getId(), false, attrs);
/*     */     
/* 240 */     Account acct2 = Provisioning.getInstance().get(Key.AccountBy.name, TestUtil.getAddress(USER2_NAME));
/* 241 */     Mailbox remoteMbox = MailboxManager.getInstance().getMailboxByAccount(acct2);
/* 242 */     Mailbox mbox1 = MailboxManager.getInstance().getMailboxByAccount(acct);
/* 243 */     remoteMbox.grantAccess(null, 7, acct.getId(), (byte)1, (short)7, null);
/* 244 */     mbox1.grantAccess(null, 7, acct2.getId(), (byte)1, (short)7, null);
/*     */     
/* 246 */     zmbx.moveContact(contact.getId(), acct2.getId() + ":" + 7);
/* 247 */     ZMailbox remoteZmbx = TestUtil.getZMailbox(USER2_NAME);
/* 248 */     String idStr = (String)TestUtil.search(remoteZmbx, "in:Contacts testMoveContact", "contact").get(0);
/* 249 */     Contact ct = remoteMbox.getContactById(null, Integer.parseInt(idStr));
/*     */     
/* 251 */     List<Contact.Attachment> list = ct.getAttachments();
/* 252 */     Assert.assertFalse(list.isEmpty());
/* 253 */     Contact.Attachment att = (Contact.Attachment)list.get(0);
/* 254 */     Assert.assertEquals("file1.txt", att.getFilename());
/* 255 */     Assert.assertEquals("text/plain", att.getContentType());
/* 256 */     Assert.assertEquals("attachment 1", new String(att.getContent()));
/*     */     
/*     */ 
/* 259 */     remoteZmbx.moveContact(String.valueOf(ct.getId()), acct.getId() + ":" + 7);
/*     */     
/* 261 */     remoteMbox.revokeAccess(null, 7, acct.getId());
/* 262 */     mbox1.revokeAccess(null, 7, acct2.getId());
/*     */     
/* 264 */     idStr = (String)TestUtil.search(zmbx, "in:Contacts testMoveContact", "contact").get(0);
/*     */     
/* 266 */     ct = mbox1.getContactById(null, Integer.parseInt(idStr));
/*     */     
/* 268 */     list = ct.getAttachments();
/* 269 */     Assert.assertFalse(list.isEmpty());
/* 270 */     att = (Contact.Attachment)list.get(0);
/* 271 */     Assert.assertEquals("file1.txt", att.getFilename());
/* 272 */     Assert.assertEquals("text/plain", att.getContentType());
/* 273 */     Assert.assertEquals("attachment 1", new String(att.getContent()));
/*     */   }
/*     */   
/*     */   private byte[] getAttachmentData(ZContact contact, String attachmentName) throws Exception
/*     */   {
/* 278 */     InputStream in = contact.getAttachmentData(attachmentName);
/* 279 */     return ByteUtil.getContent(in, 0);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void testImportContacts()
/*     */     throws Exception
/*     */   {
/* 287 */     int timeout = 60000;
/* 288 */     long contactNum = 1L;
/* 289 */     String folderId = Integer.toString(7);
/* 290 */     ZMailbox mbox = TestUtil.getZMailbox("user1");
/* 291 */     String csvText = "\"email\",\"fullName\"\n\"testImportContacts@example.org\",\"" + NAME_PREFIX + " testImportContacts\"";
/* 292 */     String attachmentId = mbox.uploadAttachment("ImportContacts.csv", csvText.getBytes(), "text/plain", timeout);
/*     */     
/* 294 */     ZMailbox.ZImportContactsResult res = mbox.importContacts(folderId, "csv", attachmentId);
/* 295 */     Assert.assertEquals("Number of contacts imported", contactNum, res.getCount());
/*     */   }
/*     */   
/*     */   public void tearDown() throws Exception
/*     */   {
/* 300 */     TestUtil.setAccountAttr("user1", "zimbraContactMaxNumEntries", this.mOriginalMaxContacts);
/* 301 */     cleanUp();
/*     */   }
/*     */   
/*     */   private void cleanUp() throws Exception
/*     */   {
/* 306 */     TestUtil.deleteTestData("user1", NAME_PREFIX);
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws Exception
/*     */   {
/* 311 */     TestUtil.cliSetup();
/* 312 */     TestUtil.runTest(TestContacts.class);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestContacts.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */